<?php 
	echo $this->Html->script('jquery.filedrop');
	echo $this->Html->script('upload_utils');
	echo $this->Html->css('upload');
	// print_r($user);
?>

<ul class="breadcrumb">
  <li><a href="<?php echo $this->webroot;?>">Home</a><span class="divider">/</span></li>
  <li class="active"><?php echo __("Profilo"); ?></li>
</ul>

<form action="<?php echo $this->webroot;?>Users/save" method="post" id="form" enctype='multipart/form-data'>
	<div class="row-fluid">
			<div class="span3" id='fotoContainer'>
			    <div id="dropbox">
			        <span id="uploadMessage" class="message"><i class="icon-download icon-3x"></i><br><?php echo __("Trascina o premi per caricare un'immagine");?> <br /></span>
				  	<a href="#" class="thumbnail">
				  		<?php if (!empty($user['Attach']['Anagrafica']['foto'])) { ?>
							<img src="<?php echo $this->webroot.$user['Attach']['Anagrafica']['foto']?>" class='img-rounded' />
				  		<?php } else { ?>
							<img src="<?php echo $this->webroot.'img/utente.jpg'?>" style='width: 100px;height: 100px' class='img-rounded' />
				  		<?php } ?>
				    </a>
			    </div>
			    <input type="hidden" name="uploadPermission" id="uploadPermission" value="<?php echo $this->Permission->check("controllers/Upload/ajaxUpload") ?>" />
			    <input type="hidden" name="newFoto" id="newFoto" value="" />
				<div class="progressHolder">
					<div id="progress" class="progress"></div>
					<div class="name_pic"></div>
				</div>
				<!-- Upload file -->
			    <input type="hidden" id="url" value='<?php echo  $this->webroot; ?>upload/ajaxUpload' />

			</div>
			<div class="span9">
				<div class="tabbable"> <!-- Only required for left/right tabs -->
				  <ul class="nav nav-tabs">
				    <li class="active"><a href="#tab1" data-toggle="tab">Utente</a></li>
				    <li><a href="#tab2" data-toggle="tab">Anagrafica</a></li>
				  </ul>
				  <div class="tab-content">
				    <div class="tab-pane active" id="tab1">
						<fieldset>
						    <input type='hidden' name='User[id]' value='<?php echo $user['User']['id'];?>' />
						    <input type='hidden' name='view' value='profilo' />
						    <label>Username</label> <input type="text" name="User[username]" value="<?php echo $user['User']['username'];?>" placeholder="Username" disabled>
						    <label>Email</label> <input type="text" name="User[email]" value="<?php echo $user['User']['email'];?>" placeholder="Email" >
						    <label>Gruppo</label>
							<select name='User[group_id]'>
								<option value=''></option>
								<?foreach ($groups as $key => $group) {?>
							  			<option value='<? echo $key?>' <? if($key == $user['User']['group_id']) echo " selected='selected' " ?>  ><? echo $group ?></option>
								<? } ?> 
							</select> 
						    <label>Ultimo accesso</label> <input type="text" name="User[dt_accesso]" value="<?php echo $user['User']['dt_accesso'];?>" placeholder="" disabled>
						    <label>Scadenza</label> <input type="text" name="User[dt_scadenza]" value="<?php echo $user['User']['dt_scadenza'];?>" placeholder="" disabled>
						    <label>Stato</label>
							<select name='User[stato]'>
								<?foreach ($stati as $key => $stato) {?>
							  			<option value='<? echo $key?>' <? if($key == $user['User']['stato']) echo " selected='selected' " ?>  ><? echo $stato ?></option>
								<? } ?> 
							</select> 
						</fieldset>
				    </div>
				    <div class="tab-pane" id="tab2">
						<fieldset>
						    <input type='hidden' name='Anagrafica[id]' value='<?php echo $user['Anagrafica']['id'];?>' />
						    <label>Nome</label> <input type="text" name="Anagrafica[nome]" value="<?php echo $user['Anagrafica']['nome'];?>" placeholder="nome" >
						    <label>Cognome</label> <input type="text" name="Anagrafica[cognome]" value="<?php echo $user['Anagrafica']['cognome'];?>" placeholder="Cognome" >
						    <label><?echo __("Comune")?></label>
							<select name='Anagrafica[comune_id]' id="comune" style="width: 240px;">
								<option value=''></option>
								<?foreach ($comuni as $comune) {?>
							  			<option data-idprovincia='<?php echo $comune['Comune']['provincia_id']?>' value='<? echo $comune['Comune']['id']?>' <? if($comune['Comune']['id'] == $user['Anagrafica']['comune_id']) echo " selected='selected' " ?>  ><? echo $comune['Comune']['descrizione'] ?></option>
								<? } ?> 
							</select> 
							<label style="display: inline;padding-left: 20px;" id="provincia_regione"  ></label>				
						</fieldset>
				    </div>
				  </div>
				</div>
			    <p>
					<button type="submit" class="btn btn-primary"><i class='icon-ok'></i></button>
				</p>
				<?php unset($user); ?> 
			</div>
	</div>
</form>

    
    
<script>
	$(document).ready(function() {
		 $("#comune").select2({minimumInputLength: 3, placeholder: '<?php echo __("Seleziona un comune") ?>',allowClear: true}); 
		 $("#comune").change(function(){
		 	if($("#comune").val()=="")return;	
		 	 idprovincia=$('#comune :selected').data("idprovincia");
			 $.getJSON('<?php echo $this->webroot;?>Utils/regioneProvincia/'+idprovincia, function(data) {
		            $("#provincia_regione").html(data["Provincia"]["sigla"]+" - "+data["Regione"]["descrizione"]);
        	});
		 });
   });
</script>

